﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using TVXD23.Web.Models.ViewModel;

namespace TVXD23.Web.Models
{
    public class HRMRepositoty
    {
        public TVXD23Entities Db = new TVXD23Entities();
        public TVXD23Entities DB
        {
            get
            {
                return Db;
            }
        }
        // Employee
        public List<Employee> getAllEmployees()
        {
           return Db.Employees.ToList();            
        }

        public List<Employee> getListEmployeesByDeparment(int departmentId)
        {
            return Db.Employees.Where(m=>m.departmentId == departmentId).ToList();
        }

        public Employee getEmployeeById(int employeeId)
        {
            return Db.Employees.Where(m=>m.employeeId == employeeId).FirstOrDefault();
        }

        public void addNewEmployee(Employee employee)
        {
            Db.Employees.AddObject(employee);
            Db.SaveChanges();
        }

        public void deleteEmployee(int id)
        {
            Employee dm = Db.Employees.Where(m => m.employeeId == id).FirstOrDefault();
            Db.Employees.DeleteObject(dm);
            Db.SaveChanges();
        }

        public void editEmployee(EmployeesModel emp)
        {
            Employee oldEmp = Db.Employees.FirstOrDefault(m => m.employeeId == emp.employeeId);
            if (oldEmp == null) return;
            oldEmp.FullName = emp.FullName;
            //oldEmp.FirstName = emp.FirstName;
           // oldEmp.LastName = emp.LastName;
            oldEmp.Sex = emp.Sex;
            //oldEmp.Age = emp.Age;
            oldEmp.Address = emp.Address;
            oldEmp.Email = emp.Email;
            oldEmp.PhoneNumber = emp.PhoneNumber;

            oldEmp.Skills = emp.Skills;
            oldEmp.Languages = emp.Languages;
            oldEmp.Educations = emp.Educations;
            oldEmp.WorkExperiences = emp.WorkExperiences;
            oldEmp.departmentId = emp.departmentId;
            oldEmp.Jobs = emp.Jobs;
            oldEmp.ShortImage = emp.ShortImage;
            oldEmp.Birthdate = emp.Birthdate;

            Db.SaveChanges();
        }

        public static Employee convertModelToData(EmployeesModel emp)
        {
            Employee oldEmp = new Employee() { FullName = emp.FullName,  Sex = emp.Sex, Birthdate = emp.Birthdate,
                Address = emp.Address, Email = emp.Email, PhoneNumber = emp.PhoneNumber, Skills = emp.Skills, Languages = emp.Languages, 
                Educations = emp.Educations, WorkExperiences = emp.WorkExperiences, departmentId = emp.departmentId, Jobs = emp.Jobs,
                ShortImage = emp.ShortImage };           

            return oldEmp;
        }

        public static EmployeesModel convertDataToModel(Employee emp)
        {
            EmployeesModel oldEmp = new EmployeesModel()
            {
                FullName = emp.FullName,  
                Sex = emp.Sex, Address = emp.Address, Email = emp.Email, PhoneNumber = emp.PhoneNumber, 
                Skills = emp.Skills, Languages = emp.Languages, Educations = emp.Educations, employeeId = emp.employeeId, 
                WorkExperiences = emp.WorkExperiences, departmentId = emp.departmentId, Jobs = emp.Jobs, ShortImage = emp.ShortImage };
            if (emp.Birthdate != null) {
                oldEmp.Birthdate = (DateTime)emp.Birthdate;
            }

            return oldEmp;
        }
        
        // Department
        public List<Department> getAllDepartments()
        {
            return Db.Departments.ToList();
        }

        public Department getDepartmentById(int departmentId)
        {
            return Db.Departments.Where(m => m.departmentId == departmentId).FirstOrDefault();
        }

        public void addNewDepartment(Department department)
        {
            Db.Departments.AddObject(department);
            Db.SaveChanges();
        }
        public void deleteDepartment(int id)
        {
            Department dm = Db.Departments.Where(m => m.departmentId == id).FirstOrDefault();
            Db.Departments.DeleteObject(dm);
            Db.SaveChanges();
        }
        public void editDepartment(DepartmentsModel dep)
        {
            Department oldDep = Db.Departments.FirstOrDefault(m => m.departmentId == dep.departmentId);
            if (oldDep == null) return;
            oldDep.Name = dep.Name;
            oldDep.Description = dep.Description;            
            Db.SaveChanges();
        }


    }
}